package eerbaar.v2.gamebuilder.server;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import eerbaar.game.IGame;
import eerbaar.server.gamebuilder.Builder;
import eerbaar.shared.exceptions.GameException;
import eerbaar.shared.persist.ISerializerService;
import eerbaar.shared.service.ServiceFactory;

public class GameUtil {

    public static IGame loadGame() throws GameException {

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        return loadGame(user.getEmail());
    }

    public static IGame loadGame(String email) throws GameException {

        IGame result = null;

        DatastoreService service = DatastoreServiceFactory
                .getDatastoreService();

        Key key = KeyFactory.createKey("email", email);

        Entity entity = null;
        try {
            entity = service.get(key);
        } catch (EntityNotFoundException e) {
            // ignore
        }

        ISerializerService ser = ServiceFactory
                .getService(ISerializerService.class);

        if (entity != null) {

            Text text = (Text) entity.getProperty("text");
            String data = text.getValue();
            if (data != null) {
                try {
                    result = ser.load(IGame.class, data);
                } catch (Exception e) {
                    e.printStackTrace();
                    result = new Builder().buildGame();
                }
            }
        } else {
            result = new Builder().buildGame();

            String xml = ser.save(IGame.class, result);
            Text text = new Text(xml);
            entity = new Entity(key);
            entity.setProperty("text", text);
            service.put(entity);
        }

        return result;
    }

    public static void saveGame(IGame game) throws GameException {

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        Email email = new Email(user.getEmail());
        saveGame(email.getEmail(), game);
    }

    public static void saveGame(String email, IGame game) throws GameException {

        DatastoreService service = DatastoreServiceFactory
                .getDatastoreService();

        Key key = KeyFactory.createKey("email", email);

        ISerializerService ser = ServiceFactory
                .getService(ISerializerService.class);

        Entity entity = new Entity(key);

        String xml = ser.save(IGame.class, game);
        Text text = new Text(xml);
        entity.setProperty("text", text);
        service.put(entity);
    }

    private GameUtil() {
        //
    }

}
